package iceportal1

class LoginController {

    static allowedMethods = [authenticate: "POST"]

    def index = { }

    def authenticate = {
        def userKey = "0749e329-9928-4e4e-b70c-243066ee0c22"+":"+params.username+":"+params.password
        session.authKey = "Basic "+userKey.encodeAsBase64()
        session.loginid = params.username
        try {          
          withRest(uri: "http://localhost:8080/iceapi/services/") {
              get(path:"${session.systempath}/validate",
                  headers:['Authorization':session.authKey])
          }          
          redirect(controller: session.systempath, action: "main") 
        } catch (e) {
          def loginInstance = new Login(params)
          loginInstance.errors.rejectValue ("username","Login failed")
          render(view: "index", model: [loginInstance: loginInstance])
        }
        

    }
    
    def logout = {
    	String sessPath = session.systempath
    	session.invalidate()
    	redirect(controller:sessPath)
    }
}
